//============================================================================
// Name        : Individual.cpp
// Author      : Andrey Bicalho Santos e Clayson Sandro Celes
// Version     :
// Copyright   : Your copyright notice
// Description : Genetic Algorithm in C++, Ansi-style
//============================================================================
/*
 * Individual.cpp
 *
 *  Created on: 08/09/2011
 *      Author: clayson and andrey
 */

#include "Individual.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;

/**/
Individual::Individual() {
	// TODO Auto-generated constructor stub
	my_fitness = chromosome_length = 0;
	chromosome = NULL;
}

/*
 * constructor to initialize an individual in random way
 * */
Individual::Individual(int num_genes){
	//add code here
	my_fitness = 0;
	srand(time(NULL));
	//chromosome length, the number of genes
	chromosome_length = num_genes;
	Initialize();

}


void Individual::pera(){
	string x; cin >> x;
}
/*
 * initialize an individual in random way
 * */
void Individual::Initialize(){
	if(chromosome_length <= 0){
		printf("\nError in Initialize! chromosome length = %d, necessary initialization...\n",chromosome_length);
	}
	else{
		chromosome = new int[chromosome_length];
		//initiates individual randomly
		for(int i=0;i<chromosome_length;i++){
			if(rand() % 100 < 50)
				chromosome[i] = 0;
			else chromosome[i] = 1;
		}
		my_fitness = 0;
	}
}

/**/
void Individual::ReInitialize(){
	if(chromosome){
		delete[] chromosome;
		if(chromosome_length <= 0){
				printf("\nError in ReInitialize! chromosome length = %d, necessary initialization...\n",chromosome_length);
				printIndividual();
				//chromosome_length = 2;
				//Initialize();
		}
		else{
			chromosome = new int[chromosome_length];
			//initiates individual randomly
			for(int i=0;i<chromosome_length;i++){
				if(rand() % 100 < 50)
					chromosome[i] = 0;
				else chromosome[i] = 1;
			}
			my_fitness = 0;
		}
	}
	else printf("\nWarning! chromosome have not been initialized...\n");


}

/**/
void Individual::setChromosome(int *chrom){
	for(int i=0;i<chromosome_length;i++)
		chromosome[i] = chrom[i];
}

/**/
int *Individual::getChromosome(){
	return chromosome;
}


/**/
int Individual::getNumGenes() {
	//add code here
	return chromosome_length;
}

/**/
void Individual::setNumGenes(int num_genes){
	//add code here
	chromosome_length = num_genes;
}

/**/
void Individual::setGene(int index, int value) {
	//add code here
	chromosome[index] = value;
}

/**/
int Individual::getGene(int index) {
	//add code here
	return chromosome[index];
}

/**/
double Individual::getFitness() {
	//add code here
	return my_fitness;
}

/**/
void Individual::setFitness(double fitness) {
	//add code here
	my_fitness = fitness;
}

/*
 * Increases individual's length by a size defined by increase_value
 * random genes are created
 * */
void Individual::IncreaseIndividual(int increase_value){
	//add code here
	int *temp_chrom;

	temp_chrom = chromosome;

	chromosome = NULL;

	chromosome_length = chromosome_length + increase_value;
	chromosome = new int[chromosome_length];

	for(int i=0;i<chromosome_length;i++){
		if(i < (chromosome_length - increase_value))
			chromosome[i] = temp_chrom[i];
		else{
			if(rand() % 100 < 50)
				chromosome[i] = 0;
			else chromosome[i] = 1;
			}
		}

	//printf("\nIncreasing Individual\n"); pera();

	delete[] temp_chrom;
}

/*
 * Decrease individual's length by a size defined by decrease_value
 * genes are at the end will be removed
 * */
void Individual::DecreaseIndividual(int decrease_value){
	//add code here
	int *temp_chrom;

	temp_chrom = chromosome;

	chromosome = NULL;

	chromosome_length = chromosome_length - decrease_value;
	chromosome = new int[chromosome_length];

	for(int i=0;i<chromosome_length;i++){
		chromosome[i] = temp_chrom[i];
	}

	//printf("\nDecreasing Individual\n"); pera();
	delete[] temp_chrom;
}

/**/
void Individual::printIndividual() {
	//add code here
	printf("\n");
	for(int i=0;i<chromosome_length;i++)
		printf("%d",chromosome[i]);
	printf("\n");
}

/**/
Individual::~Individual() {
	// TODO Auto-generated destructor stub
	//printf("\nDestrutor da Classe Individual\n"); string x; cin >> x;
	if (chromosome) delete[] chromosome;
}

